/* eslint-disable no-const-assign */
import IMCore from './IMCore'
const ws_protocol = 'ws' // ws 或 wss
const ip = '47.106.174.31'
const port = 9326

const heartbeatTimeout = 30000 // 心跳超时时间，单位：毫秒
const reconnInterval = 1000 // 重连间隔时间，单位：毫秒

const binaryType = 'blob' // 'blob' or 'arraybuffer';//arraybuffer是字节
var handler = null
/**
 * 生成从minNum到maxNum的随机数
 * @param {*} minNum
 * @param {*} maxNum
 */
const randomNum = (minNum, maxNum) => {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10)

    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10)

    default:
      return 0
  }
}
const IMServer = {
  ws: null,
  /**
   * 初始化
   * @param h
   */
  init: function(h) {
    handler = h
  },
  /**
   * 添加一个消息监听器
   * @param key
   * @param h
   */
  addNewMessagHeandel(key, h) {
    handler.addNewMessagHeandel(key, h)
  },
  /**
   * 连接
   * @param token
   */
  connect: function(token) {
    var params = ''
    if (token) {
      params = 'token=' + token
    }
    IMCore.init(ws_protocol, ip, port, params, handler, heartbeatTimeout, reconnInterval, binaryType)
    this.ws = IMCore.connect('')
  }
}
export default IMServer

